package com.sevencar.activity;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.example.lightble.R;
import com.example.lightble.db.SettingDatabase;
import com.sevencar.activity.LightGroupAddItem.onChecedChangeListener;

public class LightGroupAddActivity extends Activity implements onChecedChangeListener{

	LinearLayout mLinearLayout;
	ArrayList<BluetoothDevice> mDeviceArray;
	Button mBtnAdd;
	Button mBtnCancel;
	EditText mEtName;
	ArrayList<String > mAddedArray;
	String strOldName;  //之前的名字
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.light_group_add_layout);
		
		
		
		
		
		mLinearLayout=(LinearLayout)findViewById(R.id.id_light_group_add_layout);
		mBtnCancel=(Button)findViewById(R.id.id_light_group_add_cancelbtn);
		mBtnAdd=(Button)findViewById(R.id.id_light_group_add_addbtn);
		mEtName=(EditText)findViewById(R.id.id_light_group_add_name);
		
		mBtnCancel.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				LightGroupAddActivity.this.finish();
			}
		});
		mBtnAdd.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				addToDataBase();
			}
		});
		
		//init data---------------------
		strOldName = getIntent().getStringExtra("GroupName");
		if(strOldName!=null){
			mEtName.setText(strOldName);
		}
		
		
		if(HomeFragment.mBLEServiceInstance!=null){
			mDeviceArray=HomeFragment.mBLEServiceInstance.getScanedDeviceArray();
		}
		
		for (BluetoothDevice  item : mDeviceArray) 
		{
			if(checkDevice(item.getAddress()))
				continue;
			LightGroupAddItem  layoutItem=new LightGroupAddItem(LightGroupAddActivity.this, item);
			layoutItem.setOnChecedChangeListener(this);
			mLinearLayout.addView(layoutItem);
		}
		
		
		
		mAddedArray=new ArrayList<String>();
	}
	public boolean checkDevice(String device){
		
		SettingDatabase sdb = new SettingDatabase(this,0);
		
		List<Map<String, String>> hhhlist=sdb.query_maplist("select  * from table_group where device_address = '"+device+"' ");
		sdb.close();
		return hhhlist.size()>0? true:false;
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
	}

	@Override
	public void onCheckedChanged(LightGroupAddItem view,String address, boolean isChecked) {
		
		if(isChecked)  //增加.
		{
			if(mAddedArray.contains(address)==false)
			{
				mAddedArray.add(address);
			}
		}else {  //去掉
			 if(mAddedArray.contains(address)){
				 mAddedArray.remove(address);
			 }
		}
	}
	//
	@SuppressLint("NewApi")
	private void addToDataBase()
	{
		int _sum = 0;
		for(int i=0;i<mLinearLayout.getChildCount();i++)
		{
			LightGroupAddItem item=(LightGroupAddItem)mLinearLayout.getChildAt(i);
			if(item.isChecked()){
				_sum ++;
			}
		}
		
		if(_sum<1){
			Toast.makeText(this, "请至少勾选两个", Toast.LENGTH_SHORT).show();
			return;
		}
		
		SettingDatabase dataBase=new SettingDatabase(this, 1);
		//检测是否有群组名字.
		if(mEtName.getText().toString().isEmpty()){
			Toast.makeText(this, "文件名为空,请输入群组名", Toast.LENGTH_SHORT).show();
			return ;
		}
		String strName=mEtName.getText().toString();
//		List<String>  groupNameList=dataBase.getGroupNamelists();
		List<Map<String, String>>  groupNameList=dataBase.query_maplist("select * from table_group where group_name ='"+strName+"' ");
		
		if(strOldName==null && groupNameList.size()>=1){  //新建的名字,但名字和别的组重名.
			Toast.makeText(this, "群组名字和别群组重复,请重新输入名字.", Toast.LENGTH_SHORT).show();
			return;
		}
		
		if(strOldName!=null){  //如果是修改,则删除之前的群组信息.
//			dataBase.deleteGroupNamelistItem(strOldName);
//			dataBase.deleteGroupTable(strOldName);
		}
	
		//增加到database
//		dataBase.newGroupTable(strName);
		boolean addedtrue = true;
		String group_code = "G_code"+System.currentTimeMillis(); 
		for(int i=0;i<mLinearLayout.getChildCount();i++)
		{
			LightGroupAddItem item=(LightGroupAddItem)mLinearLayout.getChildAt(i);
			
			
			if(item.isChecked()){
//				dataBase.addDeviceToGroupTable(strName, item.getDeviceAddress());
				if(!dataBase.addToGroup("insert into table_group (group_name,device_address,group_code,device_name) values ('"+strName+"','"+item.getDeviceAddress()+"','"+group_code+"','"+item.getDeviceName()+"')")
				){
					addedtrue = false;
				}
			}
		}
		dataBase.close();
		if(addedtrue){
			Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
			finish();
		}
		
	}

	
}
